<template>
  <pro-layout
    :menus="menus"
    :i18nRender="i18nRender"
    :collapsed="collapsed"
    :handleMediaQuery="handleMediaQuery"
    :handleCollapse="handleCollapse"
    v-bind="settings"
  >
    <a-layout-content>
      <MultiTab v-show="$store.state.app.showTab"></MultiTab>
    </a-layout-content>

    <template v-slot:menuHeaderRender>
      <div class="tac">
        <img
          v-if="collapsed"
          class="logo"
          src=""
        />
        <!--<img v-if="collapsed" src="/logo.png" />-->
        <div v-else class="white fs20 pr24">{{ sysName }}</div>
      </div>
    </template>

    <template v-slot:headerContentRender>
      <div class="mt20 diy-mbx" v-show="$store.state.app.showNav">
        <a-breadcrumb>
          <a-breadcrumb-item v-for="(t, i) in routePathArr" :key="`route${i}`">
            <span v-if="i === 0">
              <a-tooltip title="首页">
                <a @click="toPage('/')">
                  <i class="fa fa-home white" style="font-size: 18px" />
                </a>
              </a-tooltip>
            </span>
            <span v-else>{{ t.meta.title }}</span>
          </a-breadcrumb-item>
        </a-breadcrumb>
      </div>
    </template>

    <setting-drawer v-if="false" :settings="settings" @change="handleSettingChange">
      <div style="margin: 12px 0">This is SettingDrawer custom footer content.</div>
    </setting-drawer>

    <template v-slot:rightContentRender>
      <right-content @refresh="refresh" :top-menu="settings.layout === 'topmenu'" :theme="settings.theme" />
    </template>

    <template v-slot:footerRender>
      <global-footer />
    </template>
    <div class="scroll pt10 pr10 pl10" :style="{ margin: '-10px', height: `${winHeight - 130}px` }">
      <router-view v-if="isRouterActive" />
    </div>
  </pro-layout>
</template>

<script>
import { SettingDrawer, updateTheme } from '@ant-design-vue/pro-layout'
import { i18nRender } from '@/locales'
import { mapState } from 'vuex'
import storage from 'store'
import { CONTENT_WIDTH_TYPE } from '@/store/mutation-types'
import MultiTab from '@/components/MultiTab'
import RightContent from '@/components/GlobalHeader/RightContent'
import GlobalFooter from '@/components/GlobalFooter'
import { Icon } from 'ant-design-vue'

export default {
  name: 'BasicLayout',
  components: {
    SettingDrawer,
    MultiTab,
    RightContent,
    GlobalFooter,
    'a-icon': Icon
  },
  data () {
    return {
      i18nRender,
      isRouterActive: true,
      // base
      menus: [],
      // 侧栏收起状态
      collapsed: false,
      sysName: '',
      settings: {
        // 布局类型 'sidemenu'|'topmenu'
        layout: 'sidemenu',
        // 主题 'dark' | 'light'
        theme: 'light',
        primaryColor: storage.get('app.color', '#1890ff'),
        fixedHeader: true,
        fixSiderbar: true,
        colorWeak: false,
        hideHintAlert: false,
        hideCopyButton: false
      }
    }
  },
  computed: {
    ...mapState({
      // 动态主路由
      mainMenu: (state) => state.permission.addRouters,
      color: (state) => state.app.color
    }),
    routePathArr () {
      return this.$route.matched
    }
  },
  watch: {
    color (v) {
      this.setting.primaryColor = v
    }
  },
  created () {
    const routes = this.mainMenu.find((item) => item.path === '/')
    this.menus = (routes && routes.children) || []
  },
  mounted () {
    this.findConfig('sysName', data => {
      this.sysName = data.content
    })
    const userAgent = navigator.userAgent
    if (userAgent.indexOf('Edge') > -1) {
      this.$nextTick(() => {
        this.collapsed = !this.collapsed
        setTimeout(() => {
          this.collapsed = !this.collapsed
        }, 16)
      })
    }
    if (process.env.NODE_ENV !== 'production') {
      updateTheme(this.settings.primaryColor)
    }
  },
  methods: {
    refresh () {
      this.isRouterActive = false
      this.$nextTick(() => {
        this.isRouterActive = true
      })
    },
    handleMediaQuery (val) {
      if (val['screen-xs']) {
        this.collapsed = false
        this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
      }
    },
    handleCollapse (val) {
      this.collapsed = val
    },
    handleSettingChange ({ type, value }) {
      type && (this.settings[type] = value)
      switch (type) {
        case 'contentWidth':
          this.settings[type] = value
          break
        case 'layout':
          if (value === 'sidemenu') {
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fluid
          } else {
            this.settings.fixSiderbar = false
            this.settings.contentWidth = CONTENT_WIDTH_TYPE.Fixed
          }
          break
      }
    }
  }
}
</script>

<style lang="less">
@import './BasicLayout.less';
.logo {
  width: 40px;
  height: 40px;
  position: relative;
  left: -12px;
}
.float-drawer-btn {
  position: fixed;
  right: 20px;
  bottom: 100px;
  z-index: 2000;
  width: 56px;
  height: 56px;
  background: #1890ff;
  border-radius: 50%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: box-shadow 0.2s;
}
.float-drawer-btn:hover {
  box-shadow: 0 4px 16px rgba(24, 144, 255, 0.25);
  background: #40a9ff;
}
.fullscreen-drawer .ant-drawer-content {
  width: 100vw !important;
  height: 100vh !important;
  max-width: 100vw !important;
  max-height: 100vh !important;
}

</style>
<style scoped lang="less">
.iframe {
  height:calc(100% - 10px);
  margin:0 4px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.25);
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.1);
  backdrop-filter: blur(4px);
  border: 1px solid rgba(255, 255, 255, 0.18);

  iframe{
    border:none;
    outline: none;
  }
}

/deep/.ant-pro-fixed-header{
    position: inherit !important;
}
/deep/.sidemenu {
  div{
    header:nth-child(1){
      display: none;
    }
    .ant-pro-fixed-header{
      width:auto !important;
    }
  }
}
</style>
